import { Container, Graphics } from "pixi.js";

export class ChessSet {
    g: Graphics = new Graphics();
    colorSet = [0xeeeeee, 0xdddddd];

    create(width: number, height: number, size = 10) {
        const chessSet = new Graphics();
        this.g = chessSet;

        const xNum = width! / size;
        const yNum = height! / size;
        size = size;

        const xOdd = this.getNumber(xNum, (i) => 2 * i + 1);
        const yOdd = this.getNumber(yNum, (i) => 2 * i + 1);
        const xEven = this.getNumber(xNum, (i) => 2 * i);
        const yEven = this.getNumber(yNum, (i) => 2 * i);
        this.fillMost(xEven, yOdd, xOdd, yEven, size);
        this.fillRest(xNum, size, yOdd, yEven, yNum, xOdd, xEven);
        chessSet.zIndex = -100;
    }

    /** 偶 + 奇 奇 + 偶 为黑色 奇 + 奇 偶 + 偶 为白色 */
    private fillMost(
        xEven: number[],
        yOdd: number[],
        xOdd: number[],
        yEven: number[],
        size: number
    ) {
        const blacks = [
            ...this.combineArr(xEven, yOdd),
            ...this.combineArr(xOdd, yEven),
        ];
        const whites = [
            ...this.combineArr(xOdd, yOdd),
            ...this.combineArr(xEven, yEven),
        ];
        this.drawCells(this.colorSet[0], blacks, size, size, size);
        this.drawCells(this.colorSet[1], whites, size, size, size);
    }

    private fillRest(
        xNum: number,
        size: number,
        yOdd: number[],
        yEven: number[],
        yNum: number,
        xOdd: number[],
        xEven: number[]
    ) {
        const lastX = Math.floor(xNum);
        const lastXSize = (xNum % 1) * size;
        this.drawCells(
            this.colorSet[0],
            yOdd.map((i) => [lastX, i]),
            size,
            lastXSize,
            size
        );
        this.drawCells(
            this.colorSet[1],
            yEven.map((i) => [lastX, i]),
            size,
            lastXSize,
            size
        );
        const lastY = Math.floor(yNum);
        const lastYSize = (yNum % 1) * size;
        this.drawCells(
            this.colorSet[0],
            xOdd.map((i) => [i, lastY]),
            size,
            size,
            lastYSize
        );
        this.drawCells(
            this.colorSet[1],
            xEven.map((i) => [i, lastY]),
            size,
            size,
            lastYSize
        );
        this.drawCells(
            this.colorSet[(lastX + lastY) % 2 ? 0 : 1],
            [[lastX, lastY]],
            size,
            lastXSize,
            lastYSize
        );
    }

    drawCells(
        color: number,
        position: number[][],
        size: number,
        width: number,
        height: number
    ) {
        this.g.beginFill(color);
        position.forEach(([x, y]) => {
            this.g.drawRect(x * size, y * size, width, height);
        });
        this.g.endFill();
    }
    getNumber(num: number, func: (i: number) => number) {
        return [...Array(Math.floor(num / 2)).keys()].map(func);
    }
    combineArr(a: number[], b: number[]) {
        return a.map((i) => b.map((ii) => [i, ii])).flat();
    }
    mount(layerManager: Container) {
        this.g.clear();
        this.create(layerManager.width, layerManager.height, 10);
        layerManager.addChild(this.g);
    }
    resize() {
        this.create(this.g.parent.width, this.g.parent.height, 10);
    }
}
